package com.iflytek.config;

import com.iflytek.service.CallsNumberService;
import com.iflytek.service.InfoSourceService;
import com.iflytek.service.RequestAbilityService;
import com.iflytek.util.GetFileName;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class StartupRunner implements CommandLineRunner {

    public static final Logger log = LoggerFactory.getLogger(StartupRunner.class);
    private static final String INPUT_DIR = "src/main/resources/input";
    private static final String OUTPUT_DIR = "src/main/resources/output";

    @Autowired
    private InfoSourceService infoSourceService;
    @Autowired
    private RequestAbilityService requestAbilityService;
    @Autowired
    private CallsNumberService callsNumberService;

    @Override
    public void run(String... args) {
        try {
            // 读取 input/InformationSource 目录下的文件名
            String infoSourceFile = GetFileName.getSingleFileName(INPUT_DIR + "/InformationSource");
            // 读取 input/RequestAbility 目录下的文件名
            String requestAbilityFile = GetFileName.getSingleFileName(INPUT_DIR + "/RequestAbility");

            String callNumFile =  GetFileName.getSingleFileName(INPUT_DIR + "/TheNumberOfCallsForDifferentScenarios");

            if (infoSourceFile == null || requestAbilityFile == null || callNumFile == null) {
                log.error("输入文件缺失，请确保 input/InformationSource 和 input/RequestAbility 和/TheNumberOfCallsForDifferentScenarios 目录下各有一个文件。");
                return;
            }

            // 生成输出文件路径
            String outputFilePath = OUTPUT_DIR + "/" + infoSourceFile.replace(".xlsx", "-result.xlsx");
            String outputFilePath1 = OUTPUT_DIR + "/" + requestAbilityFile.replace(".xlsx", "-result.xlsx");
            String outputFilePath2 = OUTPUT_DIR + "/" + callNumFile.replace(".xlsx", "-result.xlsx");

            log.info("开始处理 Excel 文件...");
            infoSourceService.processExcelFiles(outputFilePath);
            requestAbilityService.processExcelFiles(outputFilePath1);
            callsNumberService.processExcelFiles(outputFilePath2);
            log.info("Excel 文件处理完成！");
        } catch (Exception e) {
            log.error("处理 Excel 文件时发生错误: ", e);
        }
    }
}
